package cn.zktr.Personnel.model.service.tanyali;


import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.zktr.Personnel.model.dao.tanyali.*;
import cn.zktr.Personnel.model.mappers.tanyali.*;
import cn.zktr.Personnel.model.pojos.*;
import cn.zktr.Personnel.model.service.ouyangpeng.RetIttService;
import cn.zktr.Personnel.model.service.tantiantian.TngPojService;

/**
 * @author lenovo
 *
 */
@Service
@Transactional(rollbackOn=Exception.class)
public class EmaOutService {
	@Autowired
	private EmaOutMapper outmer;
	
	@Autowired
	private EmaOutDao outdao;

	@Autowired
	private EmaDaMapper daM;
	@Autowired
	private EmaDaDao daD;

	@Autowired
	private EmaTrtsDao trD;
	@Autowired
	private TngPojService tng;

	@Autowired
	private PerIntentionDao ionD;
	@Autowired
	private EmaTrtsMapper trtsM;
	
	@Autowired
	private RetIttService itt;
	
	public void addKaoShiJieGuo(Integer ionid) {
		PerEmaTrts Trts = trtsM.selMaxTrtsid();
		System.err.println("这里                                    "+ ionid+","+ Trts.getTrtsid());
		PerIntention ion = ionD.findById(ionid).get();
		String ionrem = ion.getIonrem();
		if(ionrem.equals("培训") || ionrem=="培训") {
			tng.fanhuijieguo(ionid, Trts.getTrtsid());
		}else if(ionrem.equals("招聘") || ionrem=="招聘") {
			itt.fanhuiksjieguo(ionid, Trts.getTrtsid());
		}else if(ionrem.equals("转职") || ionrem=="转职") {
			
		}
	}
	
	
	public Iterable<PerEmaOut> selectFebAll(){
		return outdao.findAll();
	}
	
	/**批改试卷
	 * @param ZKZREM
	 * @return
	 */
	public JSONArray pigaishijuanss(String ZKZREM) {
		return daM.pigaishijuan(ZKZREM);
	}
	
	
	/**批改试卷
	 * @param ZKZREM
	 * @return
	 */
	public JSONObject pigaishijuans(String ZKZID) {
		JSONArray json = daM.pigaishijuan(ZKZID);
		JSONObject obj = new JSONObject();
		obj.put("ZKZID", json.getJSONObject(0).get("ZKZID"));
		obj.put("ZKZREM", json.getJSONObject(0).get("ZKZREM"));
		obj.put("OUTNAME", json.getJSONObject(0).get("OUTNAME"));
		obj.put("DAID", json.getJSONObject(0).get("DAID"));
		obj.put("IONID", json.getJSONObject(0).get("IONID"));
		obj.put("OUTZONF", json.getJSONObject(0).get("OUTZONF"));
		return obj;
	}
	
	/**批改试卷
	 * @param ZKZREM
	 * @return
	 */
	public JSONArray pigaishijuan(String ZKZID) {
		JSONArray jsonArray = new JSONArray();
		
		jsonArray.add(pigaishijuans(ZKZID));
		
		JSONArray arr1 = new JSONArray();
		
		for (int i = 0; i < daM.pigaishijuan(ZKZID).size(); i++) {
			JSONObject tm = daM.pigaishijuan(ZKZID).getJSONObject(i);
			JSONObject tms = new JSONObject();
			String[] split = tm.get("TMSUB").toString().split("#");
			tms.put("TM", split[0]);
			tms.put("TMNYD", tm.get("TMNYD"));
			tms.put("TMTYPE", tm.get("TMTYPE"));
			tms.put("TMFS", tm.get("TMFS"));
			tms.put("TMTYPE", tm.get("TMTYPE"));
			tms.put("TMBDA", tm.get("TMBDA"));
			
			tms.put("DAXZT", tm.get("DAXZT"));
			tms.put("DAZHT", tm.get("DAZHT"));
			
			String s1 = tm.get("DAXZT")+"#"+tm.get("DAZHT");
			System.err.println(s1);
			String[] s2 = s1.split("#");
			System.err.println(s2[i]);
			tms.put("SXDA", s2[i]);
			
			for (int j = 1; j < split.length; j++) {
				tms.put("DA"+j, split[j]);
			}
			arr1.add(tms);
		}
		jsonArray.add(arr1);
		return jsonArray;
	}
	
	/**总分提交
	 * @param ZKZREM
	 * @return 
	 * @return
	 */
	public void zfxz(Integer DAID,String TRTSFENS) {
		PerEmaTrts tr = new PerEmaTrts();
		tr.setPerEmaDa(daD.findById(DAID).get());
		tr.setTrtsfens(TRTSFENS);
	    
		trD.save(tr);
	}
	
	/**
	 * @return 批改试卷数据
	 */
	public JSONArray pigaishijuanselect() {
		// TODO Auto-generated method stub
		return outmer.pigaishijuanselect();
	}
	
	public JSONArray shijuangerenselect(String zkzrem) {
		return outmer.shijuangerenselect(zkzrem);
	}
	
	public JSONArray shijuantimu(String zkzrem) {
		return outmer.shijuantimu(zkzrem);
	}
	
	
	/**
	 * @param 考试试卷
	 * @return
	 */
	public JSONArray selectSJ(String zkzrem){
		JSONArray sj = shijuangerenselect( zkzrem);
		JSONArray tm = new JSONArray();
		int xzt = 0;
		int zht = 0;
		for (int i = 0; i < shijuantimu(zkzrem).size(); i++) {
			JSONObject tms = shijuantimu(zkzrem).getJSONObject(i);
			String[] split = tms.get("TMSUB").toString().split("#");
			String tmss = split[0];
			tms.put("tm", tmss);
			for (int j = 1; j < split.length; j++) {
				String DA = split[j];
				tms.put("DA"+j, DA);
			}
			String tmtype=tms.get("TMTYPE").toString();
			if(tmtype=="选择题" || tmtype.equals("选择题")) {
				String[] tmbda = tms.get("TMBDA").toString().split("");
				tms.put("XZTDA", tmbda.length);
			}
			
			if(tmtype=="选择题" || tmtype.equals("选择题")) {
				xzt+=Integer.valueOf(tms.get("TMFS").toString());
			}else if(tmtype=="综合题" || tmtype.equals("综合题")) {
				zht+=Integer.valueOf(tms.get("TMFS").toString());
			}
			tm.add(tms);
		}
		sj.getJSONObject(0).put("xzt", xzt);
		sj.getJSONObject(0).put("zht", zht);
		sj.add(tm);
		return sj;
	}
}
